import { toastError } from "./ToastUtil"

const baseUrl = 'http://192.168.8.111:8111'

export interface IResVO<T = any> {
    code: number // 200 表示成功
    success: boolean // 始终会返回 true
    service: string // 请求的服务名
    msg: string
    data?: T
}

function $http<T = any>(url: string,
    data?: T,
    header?: any,
    method: 'GET' | 'POST' = 'POST') {
    return new Promise<IResVO>((resolve, reject) => {
        wx.request({
            url: baseUrl + url,
            method,
            data,
            header: { ...header, category: 4 },
            success: (res) => {
                const data = res.data as IResVO
                if (data.code === 200) {
                    return resolve(data)
                }
                toastError(data.msg)
                reject(data.msg)
            },
            fail: (err) => {
                console.log('err', err);
                toastError(err.errMsg)
                reject(err.errMsg)
            }
        })
    })
}

export default $http